package com.tepth.maintenancesystem.view;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tepth.maintenancesystem.R;
import com.tepth.maintenancesystem.adapter.NFCMACAdapter;
import com.tepth.maintenancesystem.untils.NFCInterface;
import com.tepth.maintenancesystem.untils.NFCManger;
import com.tepth.maintenancesystem.untils.ToastUtils;
import com.tepth.maintenancesystem.untils.Untils;

import java.util.Vector;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NFCListActivity extends AppCompatActivity implements NFCInterface {

    @BindView(R.id.tv_banding_nfc_mac)
    TextView tv_banding_nfc_mac;

    @BindView(R.id.lv_nfc_list)
    ListView lv_nfc_list;

    @BindView(R.id.rl_nfc_loading)
    RelativeLayout rl_nfc_loading;

    @BindView(R.id.tv_nfclist_message)
    TextView tv_nfclist_message;

    private Context context;
    private String address;
    private NFCMACAdapter mac_adapter;
    private boolean isFreshList=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nfclist);
        context = this;
        ButterKnife.bind(this);
        NFCManger.interruptDevice();
//        address = FileUtils.getNFC_MAC_File(FileUtils.getInnerSDCardPath() + "/tepth/NFCMAC.txt").trim();
//        if(address.equals("")) {
        address = Untils.getNFCMAC(context);
//        }
        handler.sendEmptyMessage(1);
        NFCManger.startScanAndGetDevicedList(NFCListActivity.this, context);
        lv_nfc_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(isFreshList)
                    return;
                address = (String) mac_adapter.getItem(position);
                Untils.setNFCMAC(context, address);
                handler.sendEmptyMessage(1);
                ToastUtils.showToast(context, "绑定成功");
                finish();
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0://显示NFC列表
                    if(isFreshList)
                        return;
                    isFreshList=true;
                    Vector<String> nfcList = (Vector<String>) msg.getData().getSerializable("nfcList");
                    if (nfcList.size() > 0) {
                        lv_nfc_list.setVisibility(View.VISIBLE);
                        rl_nfc_loading.setVisibility(View.GONE);
                        if (mac_adapter == null) {
                            mac_adapter = new NFCMACAdapter(context, nfcList);
                            lv_nfc_list.setAdapter(mac_adapter);
                            isFreshList=false;
                        } else {
                            mac_adapter.notifyDataSetChanged();
                            mac_adapter.setNfcList(nfcList);
//                            mac_adapter.notifyDataSetChanged();
                            isFreshList=false;
                        }
                    } else {
                        lv_nfc_list.setVisibility(View.GONE);
                        rl_nfc_loading.setVisibility(View.VISIBLE);
                        tv_nfclist_message.setText("正在搜索设备...");
                        isFreshList=false;
                    }
                    break;
                case 1:
                    if (address.equals("")) {
                        tv_banding_nfc_mac.setText("未绑定设备");
                    } else {
                        tv_banding_nfc_mac.setText(address);
                    }
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @OnClick({R.id.tv_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                this.finish();
                break;
        }
    }

    @Override
    public void searchDevice() {

    }

    @Override
    public void connectionDevice() {

    }

    @Override
    public void successConnectionDevice() {

    }

    @Override
    public void searchCard() {

    }

    @Override
    public void successCard(byte[] msgBuffer) {

    }

    @Override
    public void errorCard() {

    }

    @Override
    public void stopSearchCard() {

    }

    @Override
    public void updateDeviceList(Vector<String> nfcList) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("nfcList", nfcList);
        Message msg = new Message();
        msg.what = 0;
        msg.setData(bundle);
        handler.sendMessage(msg);
    }

    @Override
    protected void onDestroy() {
        NFCManger.setStopGetDevicedList(true);
        super.onDestroy();
    }
}
